package kevin.framework.basic.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import kevin.framework.basic.common.tree.ListToTree;
import kevin.framework.basic.common.ui.ClientButton;
import kevin.framework.basic.dao.IBaseDao;
import kevin.framework.basic.dao.IMenuDao;
import kevin.framework.basic.service.impl.BaseService;
import kevin.framework.basic.domain.Menu;

import kevin.framework.basic.service.IMenuService;

/**
 * Generate by Kevin's CodeBuilder
 **/
@Service
public class MenuService extends BaseService<Menu, String> implements IMenuService {

	private static final long serialVersionUID = -1L;

	private static ListToTree<Menu> toTree;

	public MenuService() {
		toTree = new ListToTree<Menu>(this);
	}

	IMenuDao menuDao;
	
	@Resource(name = "menuDao")
	@Override
	public void setDao(IBaseDao<Menu, String> dao) {
		this.dao = dao;
		menuDao = (IMenuDao)dao;
	}
	

	@Override
	public int deleteBatch(List<String> list) throws Exception {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("idList", list);
		List<Menu> listMenu = this.selectParam(param);
		//List<String> pidList = new ArrayList<String>();
		for(Menu m : listMenu) {
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("id", m.getId());
			params.put("pid", m.getPid());
			menuDao.updatePid(params);
		}
		return super.deleteBatch(list);
	}

	@Override
	public JSONArray getMenuJsonByPid(String pid) throws Exception {
		List<ClientButton> buttonlist = new ArrayList<ClientButton>();
		return this.getMenuJsonByPid(pid, buttonlist);
	}
	
	@Override
	public JSONArray getMenuJsonByPid(String pid, List<ClientButton> buttonlist) throws Exception {
		Map<String,Object> params = new HashMap<String,Object>();
		return this.getMenuJsonByPid(pid, buttonlist, params);
	}
	
	
	
	

	@Override
	public JSONArray getMenuJson(String pid, List<Menu> list) throws Exception {
		JSONArray res = new JSONArray();
		try {
			res = toTree.getJSONArray(pid, list, false);
		} catch (Throwable e) {
			logger.error("",e);
		}
		return res;
	}


	@Override
	public JSONArray getMenuJsonByPid(String pid, List<ClientButton> buttonlist,Map<String,Object> params) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, String> order = new HashMap<String, String>();
		order.put("orderIndex", "asc");
		param.put("orderColumn", order);
		List<Menu> list = this.dao.selectParam(param);
		toTree.setBtnList(buttonlist);
		boolean needTools = true;
		if(buttonlist.size() == 0) {
			needTools = false;
		}
		JSONArray res = new JSONArray();
		try {
			res = toTree.getJSONArray(pid, list, needTools);
		} catch (Throwable e) {
			logger.error("",e);
		}
		return res;
	}
	
	@Override
	public JSONArray getMenuJsonByPid(String pid, Map<String, Object> params) throws Exception {
		List<ClientButton> buttonlist = new ArrayList<ClientButton>();
		return this.getMenuJsonByPid(pid, buttonlist, params);
	}


	@Override
	public void createNode(Menu bean, JSONObject obj, Object extParams) {
		if(obj.getInteger("deep") > 1 && bean.getIsLeaf().equals(0)) {
			obj.put("closed", true);
		}
	}

	@Override
	public String getTreeId(Menu bean, Object extParams) {
		// TODO Auto-generated method stub
		return bean.getId();
	}

	@Override
	public String getTreeText(Menu bean, Object extParams) {
		// TODO Auto-generated method stub
		return bean.getMenuText();
	}

	@Override
	public String getTreePid(Menu bean, Object extParams) {
		// TODO Auto-generated method stub
		return bean.getPid();
	}

	@Override
	public boolean isTreeLeaf(Menu bean, Object extParams) {		
		return bean.getIsLeaf().equals(1);
	}

	@Override
	public ClientButton createTreeButton(ClientButton btn, Menu bean, Object extParams) {
		if(bean.getIsLeaf().equals(1) && "add".equals(btn.getCmd())) {
			return null;
		}
		return btn;
	}

}
